/**
 * 图表id列表
 * @type {string[]}
 */
let chart_list_id = ['pie', 'bar', 'scatter', 'line', 'treemap', 'wordcloud'];

/**
 * 图表管理类
 */
class ChartManager {
    constructor() {
        this.charts = {};
        this.resizeTimeout = null;
        window.addEventListener('resize', this.handleResizeDebounced.bind(this));
    }

    handleResizeDebounced() {
        if (this.resizeTimeout) {
            clearTimeout(this.resizeTimeout);
        }
        this.resizeTimeout = setTimeout(() => {
            for (const id in this.charts) {
                this.charts[id].resize({
                    animation: {
                        duration: 500,
                        easing: 'cubicInOut'
                    }
                });
            }
        }, 500);
    }

    // 渲染图表
    renderChart(id, options) {
        if (this.charts[id]) {
            this.charts[id].dispose();
        }
        const theme = localStorage.getItem('theme') || "light";
        const dom = document.getElementById(id);
        const chart = echarts.init(dom, theme);
        chart.setOption(options);
        this.charts[id] = chart;
    }

    // 销毁图表
    destroyChart(ids) {
        ids.forEach(id => {
            if (this.charts[id]) {
                this.charts[id].dispose();
                delete this.charts[id];
            }
        });
    }

    // 更新图表数据
    updateChartData(id, newData) {
        if (this.charts[id]) {
            this.charts[id].setOption(newData);
        }
    }
}

let chart_manager = new ChartManager();

function chart_init() {
    chart_manager.renderChart(chart_list_id[0], option_pie);
    chart_manager.renderChart(chart_list_id[1], option_bar);
    chart_manager.renderChart(chart_list_id[2], option_scatter)
    chart_manager.renderChart(chart_list_id[3], option_line)
    chart_manager.renderChart(chart_list_id[4], option_treemap)
    chart_manager.renderChart(chart_list_id[5], option_wordcloud)
}

/**
 * 销毁图表
 */
function chart_destroy() {
    chart_manager.destroyChart(chart_list_id);
}

chart_init()
